#include<stdio.h>
#include<stdlib.h>

void insert(int *arr, int pos, int val, int n);
void print_array(int *arr, int n);
void delete(int *arr, int pos, int n);
int *extend(int *p, int size, int enlarge);

int main() 
{
    // int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    int n = sizeof(arr)/sizeof(arr[0]);
    
    //插入值
    int pos,val;
    printf("请输入插入的pos:");
    scanf("%d", &pos);
    printf("请输入val:");
    scanf("%d", &val);

    insert(arr, pos, val, n);
    print_array(arr, n);

    //删除值
    printf("请输入删除的pos:");
    scanf("%d", &pos);
    
    delete(arr, pos, n);
    print_array(arr, n);
    n--;

    // 扩容
    int *p = (int *)malloc(10*sizeof(int));
    p = arr;
    p = extend(p, 10, 5);
    print_array(p, 15);
}

/** 插入值 */
void insert(int *arr, int pos, int val, int n)
{
    for (int i=n-1; i>pos; i--)
    {
        arr[i] = arr[i-1];
    }        
    arr[pos] = val;
}
/** 打印数组 */
void print_array(int *arr, int n)
{
    for (int i = 0; i < n; i++)
    {
        printf("%d  ", arr[i]);
    }
    printf("\n");
}
/** stdio.h里面有remove */ 
void delete(int *arr, int pos, int n)
{
    for (int i=pos; i<n-1; i++)
    {
        arr[i] = arr[i+1];
    }     
}
/** 扩容 */
int *extend(int *p, int size, int enlarge)
{
    int *nums = (int *)malloc((size+enlarge) * sizeof(int));
    for (int i = 0; i < size; i++)
    {
        nums[i] = p[i];
    }
    for (int i = size; i < size+enlarge; i++)
    {
        nums[i] = 0;
    }
    return nums;
}